(function(w, $) {
    'use strict';
    var TYPE = {
        "byte" : {name : "byte",default:1},
        "short" : {name : "short",default:1},
        "int" : {name : "int",default:1},
        "long" : {name : "long",default:1},
        "string" : {name : "string",default:"abc"},
        "struct" : {name : "struct",default : null},
        "array" : {name : "array",default : null},
    };
    //默认字段
    var COLUMNS = [
        { field: "key", title : "参数", },
        { field: "name", title : "名称", },
        { field: "type", title : "类型", },
        { field: "genericType", title : "泛型", },
        { field: "default", title : "默认值", },
        { field: "required", title : "必填", },
        { field: "describe", title : "描述", },
    ];

    var DEFAULT = {
        protocol : "http",//协议
        name : "",//名称
        path : "",//路径
        data : undefined,
        parentId : 'pid',
        children : "children",
        id : "id",
        isTree : true,//是否为数据结构数据
        columns : COLUMNS,
    }

    w.API = function(options){
        this.options = options;
        this.init();
    }

    w.API.prototype.init = function(){
       this.options =   $.extend({},DEFAULT,this.options);
    };
    w.API.prototype.mock = function (data) {
        if(!data){ return null ;}
        var result;
        //根节点
        if(!TYPE[data.type]){ //未识别类型
            result = "未识别该类型";
        }else if(TYPE[data.type].default){ //基础类型
            result =  TYPE[data.type].default;
        }else if(data.type == TYPE.array.name && TYPE[data.genericType].default){//数组并且泛型为基本类型
            result =  [TYPE[data.genericType].default] ;
        }else if(data.type == TYPE.map.name){//map
            result = {};
            for(var key in data[this.options.children]){
                var record = data[this.options.children][key];
                result[record.key] = this.mock(record);
            }
        }else if(data.type == TYPE.array.name && TYPE.map.name == data.genericType){ //数组并且泛型为map
            result = [{}];
            for(var key in data[this.options.children]){
                var record = data[this.options.children][key];
                result[0][record.key] = this.mock(record);
            }
        }
       return  result;
    };

    w.API.mock = function (options) {
        var api = new API(options);
        return api.mock(api.options.data);
    };




    w.ApiDocument = function ($el,options) {
        this.$el = $el;
        this.options = options;
        this.init();
    };

    w.ApiDocument.prototype.mock = function () {
        return  API.mock({data:this.options.data});
    };

    w.ApiDocument.prototype.init = function(){
        this.options =   $.extend({},DEFAULT,this.options);
        this.initTable();
    };

    w.ApiDocument.prototype.initTable = function(){
        var opts =  $.extend({},this.options);
        //data数据类型为基本类型或不能识别的类型
        if(!TYPE[this.options.data.type] || !TYPE[this.options.data.type].default){
            //去除最外层的数据
            var data  = opts.data = this.options.data[this.options.children];
            if(this.options.isTree){
                var tree =   Tree.addRelevance({data:data,parentId : this.options.parentId,id : this.options.id});
                opts.data =   Tree.toArray({data:tree});
            }
        }else {
            opts.data = null;
        }
        this.$el.bootstrapTreeTable(opts);
        if(opts.data){
            var data = this.$el.bootstrapTreeTable("getData");
            this.options.data[this.options.children] = Tree.toTree({data : data,parentId : this.options.parentId,id : this.options.id});
         }
    };



    $.fn.apiDocument = function (options) {
        var args = Array.prototype.slice.call(arguments);
        var $el = $(this);
        var data = $el.data("api.document");
        //初始化
        if(!data && typeof options == 'object' && options){
            data = new ApiDocument($(this),options);
            $el.data("api.document",data);
            return data;
            //调用方法
        } else if(data && typeof options == 'string' && ApiDocument.prototype.hasOwnProperty(options)){
            return data[options].call(data,args);
            //返回对象
        } else if(data && !options){
            return data;
        }
    }


}(window, jQuery));